﻿using MusicLibrary.DataModels;
using MusicLibrary.Web.DbAdapters;
using MusicLibrary.Web.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MusicLibrary.Web.Controllers
{
    public class AlbumController : ApiController
    {
        IMusicLibraryAdapter _adapter;

        
        public AlbumController()
        {
            _adapter = new MusicLibraryAdapter();
        }
        public AlbumController(IMusicLibraryAdapter adapter)
        {
            _adapter = adapter;
        }

        // /api/album
        public IHttpActionResult Get()
        {
            List<AlbumListVm> bucket = _adapter.GetAlbumList();

            return Ok(bucket);
        }

        // /api/album/detail/1
        public IHttpActionResult Get(string what, int id)
        {
            if (what.ToLower() == "detail")
            {
                AlbumDetailVm album = _adapter.GetAlbumDetail(id);
                return Ok(album);
            }
            return BadRequest();
        }

        // /api/album
        public IHttpActionResult Post([FromBody] AlbumListVm albumVm)
        {
            albumVm =_adapter.SaveNewAlbum(albumVm);
            return Ok(albumVm);
        }

        // /api/album
        public IHttpActionResult Put([FromBody] AlbumListVm albumVm)
        {
            albumVm = _adapter.UpdateAlbum(albumVm);
            return Ok(albumVm);
        }
    }
}
